// ITI 1120 Winter 2012, Lab 5, Example 3 // Name: Gilbert Arbez, Student# 1234567 import java.io.*; /** * This program finds the distance a ball thrown with fixed initial velocity * travels for various angles from the horizontal. */ class Lab5Ex3 { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY double velocity; // Initial velocity of ball (m/s) double[] ranges; // Ranges travelled for various angles (m) int theta; // anle of release int index; // for traversing array // PRINT OUT IDENTIFICATION INFORMATION System.out.println(); System.out.println("ITI 1120 Winter 2012, Lab 5, Example 3"); System.out.println("Name: Gilbert Arbez, Student# 1234567"); System.out.println(); // READ IN GIVENS System.out.println( "What is the initial velocity of the ball (m/s)?" ); velocity = ITI1120.readDouble(); // Call problem solving method ranges = calculateRanges(velocity); // PRINT OUT RESULTS AND MODIFIEDS theta = 0; while ( theta <= 90 ) { System.out.println( "For angle " + theta + " degrees, ball travels " + ranges[theta/10] + " metres." ); theta = theta + 10; } }//end main /* * Method: calculateRanges * Description: Calculate the range of thrown ball for the given velocity * at different angls. * Givens: v - initial velocity of the ball. */ public static double[] calculateRanges(double v) { // Results double[] ranges; // Reference to array of ranges travelled for various angles (m) // Intermediates int theta; // Horizontal angle of ball (degrees) double thetaRad; // Horizontal angle of ball (radians) double degToRad; // Constant: degrees to radians multiplier double g; // Constant: acceleration due to gravity (m/s^2) // Body g = 9.8; // Constant: gravity acceleration degToRad = Math.PI/180.0; // Constant: degrees -> radians ranges = new double[10]; // Note that theta is an int, while thetaRad is a double. This makes the loop reliable. theta = 0; while(theta <= 90) { thetaRad = degToRad * theta; ranges[theta/10] = 2.0*v*v*Math.cos(thetaRad)*Math.sin(thetaRad)/g; theta = theta + 10; } // return the results, a reference to the array return(ranges); } } //end class